package demo;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;

/**
 * @Author: WWQ
 * @Date: 2019/4/3 15:59
 * @Version 1.0
 */
public class TestLambda3 {
    /**
     * Predicate<T> ：断言式接口
     */
    @Test
    public void test4(){
    List<String> list=Arrays.asList("fas","faisdhf","fashdf","fasdhf","7418");

    List<String> list2=filetStr(list,(s)->s.length()>4);
        for (String str: list2) {
            System.out.println(str);
        }
    }

    public List<String> filetStr(List<String> str, Predicate<String> predicate){
        List<String> list=new ArrayList<>();
        for(String str2:str){
            if(predicate.test(str2)){
                list.add(str2);
            }
        }
        return list;
    }

    /**
     * Function<T> :函数式接口
     */

    @Test
    public void test3() {
        String str = strHander("aljsdfjiowej", (x) -> x.substring(2, 5));
        System.out.println(str.toString());

    }


    public String strHander(String str, Function<String, String> fun) {
        return fun.apply(str);
    }


    /**
     * Supplier<T>: 供给型接口
     */
    @Test
    public void test2() {
        List<Integer> list = getNumberList(10, () -> (int) (Math.random() * 100));
        System.out.println(list.size());
        //        for (Integer num2: list) {
        //            System.out.println(num2.toString());
        //        }

    }


    public List<Integer> getNumberList(int num, Supplier<Integer> supplier) {
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < num; i++) {
            Integer n = supplier.get();
            list.add(n);
        }
        return list;
    }


    /**
     * Consumer<T>:消费型接口
     */
    @Test
    public void test1() {
        happy(100, (x) -> System.out.println("消费了" + x + "元"));
    }

    public void happy(double money, Consumer consumer) {
        consumer.accept(money);
    }

}
